/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.sys.db.util;

import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Administrator
 */
public class IbatisTemplate {

    public static SqlMapClient getSqlMapClient() {
        return DbTool.getSqlMapClient();
    }

    public static void insert(String stmt, Object parameter) {
        SqlMapClient sqlMap = getSqlMapClient();
        try {
            sqlMap.startTransaction();
            System.out.println("stmt : " + stmt);
            sqlMap.insert(stmt, parameter);
            sqlMap.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                sqlMap.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void delete(String stmt, Object parameter) {
        SqlMapClient sqlMap = getSqlMapClient();
        try {
            sqlMap.startTransaction();
            System.out.println("stmt : " + stmt);
            sqlMap.delete(stmt, parameter);
            sqlMap.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                sqlMap.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void update(String stmt, Object parameter) {
        SqlMapClient sqlMap = getSqlMapClient();
        try {
            sqlMap.startTransaction();
            System.out.println("stmt : " + stmt);
            sqlMap.update(stmt, parameter);
            sqlMap.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                sqlMap.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static Object query(String stmt, Object parameter) {
        SqlMapClient sqlMap = getSqlMapClient();
        Object obj = null;
        try {
            sqlMap.startTransaction();
            System.out.println("stmt : " + stmt);
            obj = sqlMap.queryForObject(stmt, parameter);
            sqlMap.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                sqlMap.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

    public static List queryForList(String stmt, Object parameter) {
        SqlMapClient sqlMap = getSqlMapClient();
        List obj = null;
        try {
            sqlMap.startTransaction();
            System.out.println("stmt : " + stmt);
            obj = sqlMap.queryForList(stmt, parameter);
            sqlMap.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                sqlMap.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

    public static Map queryForMap(String stmt, Object param1, String keyName, String valueName) {
        SqlMapClient sqlMap = getSqlMapClient();
        Map map = null;
        try {
            sqlMap.startTransaction();
            System.out.println("stmt : " + stmt);
            map = sqlMap.queryForMap(stmt, param1, keyName, valueName);
            sqlMap.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                sqlMap.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public static <T> void insertList(String stmt, List<T> datali) {
        SqlMapClient sqlMap = getSqlMapClient();
        try {
            sqlMap.startTransaction();
            System.out.println("stmt : " + stmt);
            sqlMap.startBatch();
            for (T data : datali) {
                sqlMap.insert(stmt, data);
            }
            sqlMap.executeBatch();
            sqlMap.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                sqlMap.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static <T> void updateList(String stmt, List<T> datali) {
        SqlMapClient sqlMap = getSqlMapClient();
        try {
            sqlMap.startTransaction();
            System.out.println("stmt : " + stmt);
            sqlMap.startBatch();
            for (T data : datali) {
                sqlMap.update(stmt, data);
            }
            sqlMap.executeBatch();
            sqlMap.commitTransaction();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                sqlMap.endTransaction();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
